Home 〉 CSS Tutorials 〉 CSS Selectors Explained: Element, Class, ID, Group, Descendant & Universal | Beginner's Guide
CSS Selectors Explained: Element, Class, ID, Group, Descendant & Universal | Beginner's Guide !
Looking to enhance your web styling skills?
This tutorial delves into the core CSS selectors: Element, Class, ID, Group, Descendant, and Universal. Each selector is explained with practical examples, demonstrating how to target and style HTML elements effectively. By mastering these selectors, you'll gain control over your webpage's design and layout. For a detailed exploration and hands-on examples, refer to this blog post.
What are CSS selectors, and why are they crucial for web development? CSS selectors are patterns used to select and style elements on a webpage. They form the foundation of CSS, allowing developers to apply styles to specific HTML elements efficiently. Understanding these selectors is essential for creating well-structured and visually appealing web pages.
in the previous tutorials we have covered a short description on CSS Selectors, for more details refer to our blog post on CSS Syntax and Selectors Explained with Examples | Beginners Guide
In this Tutorial, we are going to cover the following CSS 'Simple Selectors':
Targets HTML tags directly.
<!DOCTYPE html>
<html>
<head>
<style>
label {
background-color: yellow;
}
</style>
</head>
<body>
<p>This is a HTML paragraph tag</p>
<label>This is a HTML label tag.</label> <br>
<label>This is a HTML label tag.</label>
</body>
</html>
output 📌
This is a HTML paragraph tag
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This styles all '<label>' tags.
Starts with a dot '.' and targets elements with that class.
<!DOCTYPE html>
<html>
<head>
<style>
.para {
background-color: yellow;
}
</style>
</head>
<body>
<p id="phead1">This is a HTML Paragraph head</p>
<p class="para">This is a HTML paragraph tag 1.</p>
<p class="para">This is a HTML paragraph tag 2.</p>
<p>This is a HTML paragraph tag 3.</p>
</body>
</html>
output 📌
This is a HTML Paragraph head
This is a HTML paragraph tag 1.
This is a HTML paragraph tag 2.
This is a HTML paragraph tag 3.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Starts with a hash '#' and targets a specific element with that ID.
<!DOCTYPE html>
<html>
<head>
<style>
#phead {
background-color: yellow;
}
</style>
</head>
<body>
<p id="phead">This is a HTML Paragraph head</p>
<p class="para1">This is a HTML paragraph tag 1.</p>
<p class="para1">This is a HTML paragraph tag 2.</p>
<p>This is a HTML paragraph tag 3.</p>
</body>
</html>
output 📌
This is a HTML Paragraph head
This is a HTML paragraph tag 1.
This is a HTML paragraph tag 2.
This is a HTML paragraph tag 3.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Apply the same styles to multiple elements.
<!DOCTYPE html>
<html>
<head>
<style>
span, #phead, .para {
background-color: yellow;
}
</style>
</head>
<body>
<p id="phead">This is a HTML Paragraph head</p>
<p class="para">This is a HTML paragraph tag 1.</p>
<p class="para">This is a HTML paragraph tag 2.</p>
<p>This is a HTML paragraph tag 3.</p>
<span>This is a Span</span>
</body>
</html>
output 📌
This is a HTML Paragraph head
This is a HTML paragraph tag 1.
This is a HTML paragraph tag 2.
This is a HTML paragraph tag 3.
This is a SpanYou can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Targets elements inside another element.
<!DOCTYPE html>
<html>
<head>
<style>
.div1 p {
background-color: yellow;
}
</style>
</head>
<body>
<div class="div1">
<h2>This is a HTML heading tag.</h2>
<h3>This is a another HTML heading tag.</h3>
<p id="phead">This is a HTML Paragraph head</p>
<p class="para">This is a HTML paragraph tag 1.</p>
<p class="para">This is a HTML paragraph tag 2.</p>
<p>This is a HTML paragraph tag 3.</p>
</div>
</body>
</html>
output 📌
This is a HTML Paragraph head
This is a HTML paragraph tag 1.
This is a HTML paragraph tag 2.
This is a HTML paragraph tag 3.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Targets all elements.
Example 📄
* {
box-sizing: border-box;
}
Ready to apply your knowledge of CSS selectors? With a solid understanding of Element, Class, ID, Group, Descendant, and Universal selectors, you're equipped to style your web pages with precision. These fundamental tools empower you to create clean, maintainable, and responsive designs. For more in-depth insights and advanced techniques, continue exploring our comprehensive CSS tutorials.
A class selector targets multiple elements with the same class, while an ID selector is unique and targets a single element. Use class selectors for reusable styles and ID selectors for unique elements.
The descendant selector targets all nested elements, regardless of their depth, within a specified parent. In contrast, the child selector targets only direct child elements of a parent.
The universal selector applies styles to all elements on a page. It's useful for resetting default styles or applying global styles, but use it sparingly to avoid performance issues.
Yes, grouping selectors allows you to apply the same styles to multiple elements, reducing redundancy in your CSS code. . The code   stands for space.
For a comprehensive understanding, refer to the full tutorial linked above. Additionally, the [Wikipedia page on CSS] provides valuable insights into CSS selectors and their usage.